#!/usr/bin/env pythontkinter
# -*- coding: utf-8 -*-
from RemoveBg import *
from tkinter import *
import os
import time

LOG_LINE_NUM = 0

class MY_GUI():
    def __init__(self,init_window_name, api_key = None, init_folder = None, output_folder = None):
        self.init_window_name = init_window_name
        self.api_key = api_key
        self.init_folder = init_folder
        self.output_folder = output_folder

    #设置窗口
    def set_init_window(self):
        self.init_window_name.title("文本处理工具_v1.2")           #窗口名
        self.init_window_name.geometry('400x150+800+400')                         #400 150为窗口大小，800 +400 定义窗口弹出时的默认展示位置
      
        #self.init_window_name["bg"] = "pink"                                    #窗口背景色，其他背景色见：blog.csdn.net/chl0000/article/details/7657887
        self.init_window_name.attributes("-alpha",1)                          #虚化，值越小虚化程度越高
        #标签
        self.api_key_label = Label(self.init_window_name, text="API-Key")
        self.api_key_label.grid(row=1, column=0)
        self.init_folder_lable = Label(self.init_window_name, text="待抠图文件夹")
        self.init_folder_lable.grid(row=2, column=0)
        self.output_folder_lable = Label(self.init_window_name, text="输出文件夹")
        self.output_folder_lable.grid(row=3, column=0)
        #文本框
        if self.api_key == None:
            self.api_key_text = Text(self.init_window_name, width=40, height=1)  #写入api_key
            self.api_key_text.grid(row=1, column=3)
            
        else:
            self.api_key_l = Label(self.init_window_name, text=self.api_key)   
            self.api_key_l.grid(row=1, column=3)
        if self.init_folder == None:
            self.init_folder_text = Text(self.init_window_name, width=40, height=1)  #待抠图文件夹
            self.init_folder_text.grid(row=2, column=3)
            
        else:
            self.init_folder_l = Label(self.init_window_name, text=self.init_folder)
            self.init_folder_l.grid(row=2, column=3)
        if self.output_folder == None:
            self.output_folder_text = Text(self.init_window_name, width=40, height=1)  #输出文件夹
            self.output_folder_text.grid(row=3, column=3)
            
        else:
            self.output_folder_l = Label(self.init_window_name, text=self.output_folder)
            self.output_folder_l.grid(row=3, column=3)
        #按钮
        self.str_trans_to_md5_button = Button(self.init_window_name, text="抠图开始", bg="lightblue", width=10,command=self.startMatting)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=4, column=3)

    # 调用抠图
    def startMatting(self):
        self.api_key = self.api_key_text.get(1.0, END).strip('\n')
        self.init_folder = self.init_folder_text.get(1.0, END).strip('\n')
        self.output_folder = self.output_folder_text.get(1.0, END).strip('\n')
        if self.api_key:
            rmbg = RemoveBg(self.api_key)
            dirs = os.listdir(self.init_folder)  
            # 立flag如果flag = 0就是不传，，flag = 1就是传
            flag = 0;
            for dir in dirs:
                # 判断目标文件夹是否已经有同名文件，如果有同名文件就不上传抠图
                removebg_dirs = os.listdir(self.output_folder)
                flag = 1;
                for old_name in removebg_dirs:
                    na = old_name.replace('.png','.jpg')
                    if na == dir:
                        print('文件名重复：' + old_name)
                        flag = 0;
                        break;
                if flag == 1:
                    rmbg.remove_background_from_img_file(self.init_folder + '\\' + dir, self.output_folder + '\\' + dir.replace('.jpg', '.png'))
                if flag == 0:
                    continue;    
        else:
            print("api_key不能为空或者出错")
